<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:banner:add']">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="编号" align="center" prop="rid" width="100" />
            <el-table-column label="平台" align="center" prop="platform" />
            <el-table-column label="版本" align="center" prop="version" width="100">
            </el-table-column>
            <el-table-column label="图标" align="center" prop="avatar" width="100">
                <template slot-scope="scope">
                    <image-preview :src="scope.row.avatar" :width="50" :height="50" />
                </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark">
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:banner:edit']">修改</el-button>
                    <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:banner:remove']">删除</el-button> -->
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改首页轮播图对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="版本" prop="version">
                    <el-input v-model="form.version" placeholder="请输入版本" />
                </el-form-item>
                <el-form-item label="平台" prop="platform">
                    <el-select v-model="form.platform" placeholder="请选择">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="app图标" prop="avatar">
                    <ImageUpload v-model="form.avatar" :limit="1" />
                    <p style="color: #f00; font-size: 12px;" v-if="form.type === '1'">图片尺寸：1042*360</p>
                    <p style="color: #f00; font-size: 12px;" v-if="form.type === '2'">图片尺寸：1920*500</p>
                </el-form-item>
                <el-form-item label="安装包" prop="app">
                    <FileUpload v-model="form.app" :limit="1" :fileSize="5000" :fileType="['apk']" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listAPP, addApp, upDateApp, delTeacher } from "@/api/web/app";

export default {
    name: "Banner",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 首页轮播图表格数据
            bannerList: [],
            // 弹出层标题
            title: "",
            options: [{
                value: 'Android ipad安装包',
                label: 'Android ipad安装包'
            }, {
                value: 'iOS ipad安装包',
                label: 'iOS ipad安装包'
            }],
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,

            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                app: [
                    { required: true, message: "请上传安装包", trigger: "change" }
                ],
                // desc: [
                //     { required: true, message: "请输入描述", trigger: "blur" }
                // ]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询首页轮播图列表 */
        getList() {
            this.loading = true;
            listAPP(this.queryParams).then(response => {
                this.bannerList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                forced: 0,

            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },

        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.rid)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加安装包";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.rid || this.ids
            // upDateApp(id).then(response => {
            //     console.log(response,'respome')
                this.form = row;
                this.open = true;
                this.title = "修改安装包信息";
            // });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.rid != null) {
                        upDateApp(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        console.log(this.form, 'form');
                        addApp(this.form).then(response => {
                            console.log(response, 'res');
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.rid || this.ids;
            this.$modal.confirm('是否确认删除首页轮播图编号为"' + ids + '"的数据项？').then(function () {
                return delTeacher(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/banner/export', {
                ...this.queryParams
            }, `banner_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
  